package com.weimei.conversionTreasure.weight;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;


import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.utils.ValidateUtils;

import java.lang.ref.WeakReference;

/**
 * Created by Walter on 2019/6/14.
 * shiys
 */

public class DialogUtils extends Dialog implements DialogInterface.OnCancelListener {
    private WeakReference<Context> mContext = new WeakReference<>(null);
    private volatile static DialogUtils sDialog;
    private static TextView tvMessage;

    public DialogUtils(@NonNull Context context, String message) {
        super(context, R.style.CustomProgressDialog);
        init(context, message);
    }

    private void init(Context context, String message) {
        if (mContext.get() == null) {
            mContext = new WeakReference<>(context);
            @SuppressLint("InflateParams")
            View view = LayoutInflater.from(mContext.get()).inflate(R.layout.dialog, null);
            tvMessage = (TextView) view.findViewById(R.id.tv_message);
            tvMessage.setText(ValidateUtils.isCheck(message));
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            addContentView(view, lp);
            setCancelable(false);
            setCanceledOnTouchOutside(false);
            setOnKeyListener(new OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    return true;
                }
            });
            setOnCancelListener(this);
        }
    }

    @Override
    public void onCancel(DialogInterface dialog) {

    }

    public static synchronized void setTitleMessage(String message) {
        if(sDialog != null){
            if (tvMessage != null) {
                tvMessage.setText(message);
            }
        }
    }


    public static synchronized void showLoading(Context context) {
        showLoading(context, "loading...");
    }

    public static synchronized void showLoading(Context context, String message) {
        showLoading(context, message, true);
    }

    public static synchronized void showLoading(Context context, String message, boolean cancelable) {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }

        if (context == null || !(context instanceof Activity)) {
            return;
        }
        sDialog = new DialogUtils(context, message);
        sDialog.setCancelable(cancelable);

        if (sDialog != null && !sDialog.isShowing() && !((Activity) context).isFinishing()) {
            sDialog.show();
        }
    }

    public static synchronized void stopLoading() {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }
        if(tvMessage != null){
            tvMessage = null ;
        }
        sDialog = null;
    }

}
